package com.example.ishoopingcaffe;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.example.AppcConfig.Common;
import com.example.listener.ActionEventConstantSearch;
import com.example.listener.OnEventControlListener;
import com.example.views.cart.ShoppingCartItem;
import com.example.views.discovery.DialogDetailCommentProducts;
import com.example.views.discovery.ViewDetailReadmoreProducts;
import com.example.views.search.ViewSearchFilterMain;
import com.example.views.search.ViewSearchMain;

public class SearchActivity extends BaseActivity implements OnEventControlListener {
	LinearLayout container_search;
	Context mContext;
	Animation animation;
	
	ViewSearchMain viewSearchMain;
	ViewSearchFilterMain viewSearchFilterMain;
	
	ViewDetailReadmoreProducts viewDetailReadmoreProducts;
	DialogDetailCommentProducts dialogDetailCommentProducts;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		mContext = this;
		container_search= (LinearLayout) findViewById(R.id.container_search);
		
		viewSearchMain = new ViewSearchMain(this, this);
		viewSearchMain.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		container_search.addView(viewSearchMain);
	}

	@Override
	public void onEvent(int eventType, View control, Object data) {
		switch (eventType) {
		case ActionEventConstantSearch.ACTION_CHANGE_VIEW_MAIN_SEARCH:
		{
			viewSearchMain = new ViewSearchMain(mContext, this);
			viewSearchMain.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			animation = AnimationUtils.loadAnimation(mContext, R.anim.rail_from_right_to_left);
			viewSearchMain.startAnimation(animation);
			container_search.removeAllViews();
			container_search.addView(viewSearchMain);
			break;
		}
		case ActionEventConstantSearch.ACTION_CHANGE_VIEW_FILTER:
		{
			viewSearchFilterMain = new ViewSearchFilterMain(mContext, this);
			viewSearchFilterMain.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			animation = AnimationUtils.loadAnimation(mContext, R.anim.rail_from_right_to_left);
			viewSearchFilterMain.startAnimation(animation);
			container_search.removeAllViews();
			container_search.addView(viewSearchFilterMain);
			break;
		}
		case ActionEventConstantSearch.ACTION_CHANGE_VIEW_SEARCH_DETAILS_PPRODUCT:
		{
//			container_search.removeAllViews();
//			viewDetailSearchProducts = new ViewDetailSearchProducts(SearchActivity.this, this);
//			viewDetailSearchProducts.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//			animation = AnimationUtils.loadAnimation(mContext, R.anim.rail_from_right_to_left);
//			viewDetailSearchProducts.startAnimation(animation);
//			container_search.addView(viewDetailSearchProducts);
			break;
		}
			
		
		case ActionEventConstantSearch.ACTION_CHANGE_VIEW_SEARCH_DETAILS_READMORE_PPRODUCT:
		{
			container_search.removeAllViews();
			viewDetailReadmoreProducts = new ViewDetailReadmoreProducts(this, this);
			animation = AnimationUtils.loadAnimation(mContext, R.anim.rail_from_right_to_left);
			viewDetailReadmoreProducts.startAnimation(animation);
			container_search.addView(viewDetailReadmoreProducts);
			break;
		}
		case ActionEventConstantSearch.ACTION_CHANGE_VIEW_SEARCH_DETAILS_COMMENT_PPRODUCT:
		{
			container_search.removeAllViews();
			dialogDetailCommentProducts = new DialogDetailCommentProducts(this, this);
			animation = AnimationUtils.loadAnimation(mContext, R.anim.rail_from_right_to_left);
			dialogDetailCommentProducts.startAnimation(animation);
			container_search.addView(dialogDetailCommentProducts);
			break;
		}
		
		case ActionEventConstantSearch.ACTION_CHANGE_VIEW_SEARCH_DETAILS_ADD_SHOPPING_CART:
		{
			ShoppingCartItem item = (ShoppingCartItem)data;
				String price = item.price;
				// buy
				boolean isDuplicate = false;
				int vitri = 0;
//				if(Common.shoppingCart != null && Common.shoppingCart.size() > 0)
//				{
//					for(ShoppingCartItem index : Common.shoppingCart)
//					{	
//						if(index.itemId.equals(item.itemId))
//						{
//							isDuplicate = true;
//							vitri = Common.shoppingCart.indexOf(index);
//							break;
//						}
//						else
//						{
//							isDuplicate = false;
//						}
//					}
//					//
//					if(isDuplicate)
//					{
//						if(Common.shoppingCart.get(vitri).price.equals(price))
//						{
//							Common.shoppingCart.get(vitri).count ++;
//							Common.shoppingCart.get(vitri).totalItem = (Double.parseDouble(Common.shoppingCart.get(vitri).price) * Common.shoppingCart.get(vitri).count)+"";
//						}
//						else
//						{
//							Common.shoppingCart.add(item);
//						}
//					}
//					else
//					{
//						Common.shoppingCart.add(item);
//					}
//				}
//				else
				{
					Common.shoppingCart.add(item);
				}
				LoadTabGreen tab;
		 	   	tab = (LoadTabGreen)getParent();
				//tab = (LoadTabGreen)mContext;
		 	   	Common.badgeNum ++;
		 	   	tab.setBadge(Common.badgeNum);
//		 	   	QMessage.showMessageAndChangeTab(DiscoveryActivity.this, getString(R.string.textCongratulation1), getString(R.string.congratulation_order_success), 
//		 	   		getString(R.string.textGoToCart), getString(R.string.btnCancel), StaticValue.tabMyCart);
			
			
		}
		default:
			break;
		}
		
	}


}
